function Proton(n, len) {
	n = n.toString();
	var a = [];
	a[len - n.length] = n;
	return a.join("0");
};

function parseTime(n) {
	n = parseInt(n);
	if (n <= 0) {
		return "00:00:00";
	};
	return (Proton(parseInt(n / 60), 2) + ":" + Proton(n % 60, 2));
};

function FREEFunction(src) {
	var BigSrc = false,
		Ftime = new Date().getTime();
	var bc = "backgroundColor",
		contraryNUM = [1, 0];
	var namesArr = ["张三丰", "李世民", "黄蓉", "杨过"];

	if (fsoTF) {
		if (!ExistsFolder(src)) {
			fsoTF = false;
		} else {
			BigSrc = src;
		};
	}; //   init BigSrc *******************----------------
	//    ***********  open or cret init area  -------------------------
	var srcGuest = "Guests",
		gn = "/",
		Txt = ".txt";
	var guestsLength = 10,
		deskLength = 3;

	// ***********************   write json area  ----------------------------
	this.saveRoom = function(R) {
		R = (R || oSelf.on);
		cretF(BigSrc + R + gn + "oRoom" + Txt, JSON.stringify(oRoom));
	};
	this.saveSelf = function() {
		oRoom.time = Ftime;
		cretF(BigSrc + this.name + Txt, JSON.stringify(oSelf));
	};
	// ***********************   open json area -----------------
	this.openJSON = function(src) {
		var t = openF(src);
		try {
			if (t && typeof(t = JSON.parse(t)) == "object") {
				return t;
			} else {
				return {};
			};
		} catch (e) {
			return {};
		};
	};
	//************* open oRoom && oSelf area-----------
	this.openRoom = function(R) {
		return this.openJSON(BigSrc + R + gn + "oRoom" + Txt);
	};
	this.openSelf = function(Pip) {
		return this.openJSON(BigSrc + Pip + Txt);
	};
	//     *************  open guests area ---------------
	this.addGuest = function(R) {
		if (this.getGuests(R).length < guestsLength) {
			cretF(BigSrc + R + gn + srcGuest + gn + this.name + Txt, new Date().getTime());
			return true;
		} else {
			return false;
		};
	};
	this.outGuest = function(R) {
		deleteFile(BigSrc + R + gn + srcGuest + gn + this.name + Txt);
		if (this.getGuests(R).length == 0) {
			deleteFolder(BigSrc + oSelf.on);
		};
	};
	//*  ***************  getGuestlength area   -----------------
	this.getGuests = function(R) {
		var t = [];
		var f = fso.getFolder(BigSrc + R + gn + srcGuest),
			d;
		d = new Enumerator(f.Files);
		for (; !d.atEnd(); d.moveNext()) {
			t.push(d.item().name);
		};
		return t;
	};


	var xyLen = 0,
		PA = "player1",
		PB = "player2",
		PAB = {};
	PAB[PA] = PB;
	PAB[PB] = PA;
	var white = "white",
		black = "black",
		box = [],
		childrens = [],
		useRand = -1,
		onClosing = false;
	var oSelf = {},
		oRoom = {},
		json = {};
	var InRoomTim = 0,
		OutRoomTim = 0;

	oSelf = {
		Role: {},
		active: Ftime,
		on: false,
		goTiming: PA
	};
	oRoom = {
		player1: false,
		player2: false,
		ready: {},
		onGame: false,
		frees: [],
		setTimes: 300,
		gameTime: {},
		inTime: {},
		rand: 0
	};



	for (var i = 0; i < namesArr.length; i++) {
		var t = this.openSelf(namesArr[i]);
		if (!t.active || t.active < (new Date().getTime() - 10000)) {
			this.name = namesArr[i];
			oSelf.active = new Date().getTime();
			this.saveSelf();
			$E("freeBnr3").innerHTML = this.name;
			break;
		};
	};


	//    player area ****************************************
	this.CretOrOutRoom = function() {
		if (!this.name) {
			return;
		};
		if (oSelf.on) {
			//提示区域 可有可无******************↓
			if (oSelf.Role.A) {
				if (oRoom.onGame && !window.confirm("你正在进行下子，确定退出房间？")) {
					return false;
				} else if (!oRoom.onGame && !window.confirm("你已入座，确定退出房间？")) {
					return false;
				};
			};
			//提示区域 可有可无******************↑
			this.A_Guest(oSelf.on);
		} else {
			var t;
			for (var i = 0; i < deskLength; i++) {
				t = (i + 1) + "桌";
				if (!ExistsFolder(BigSrc + t)) {
					cretFD(BigSrc + t);
					cretFD(BigSrc + t + gn + srcGuest);
					this.saveRoom(t);
					this.A_Guest(t);
					this.B_Player(PA);
					break;
				};
			};
		};
	};
	this.A_Guest = function(R, typ) {
		if (!this.name || (!ExistsFolder(BigSrc + R))) {
			return false;
		};
		if (oSelf.on) {
			if (!onClosing && this.onGame) {
				alert("A_Guest:is gaming!");
				return false;
			};
			if (oSelf.Role.A) {
				this.B_Player(oSelf.Role.A);
			};
			this.outGuest(oSelf.on);
			oSelf.on = false;
			oSelf.Role = {};
			this.saveSelf();

			$E("freeBnr1").innerHTML = "创建房间";
			$E("freeBnr1").style[bc] = "#777";
			$E("freeMsg0").innerHTML = "房间";
			$E("freeMsg2").style.display = "none";
			if (onClosing) alert("A-guest:close");
			clearInterval(InRoomTim);
			OutRoomTim = setInterval(this.searchRooms.bind(this), 1000);
		} else if (R && this.getGuests(R).length < guestsLength) {
			this.addGuest(R);
			oSelf.on = R;
			oSelf.Role = {};
			this.saveSelf();

			$E("freeBnr1").innerHTML = "退出房间";
			$E("freeBnr1").style[bc] = "#F55";
			$E("freeMsg0").innerHTML = R;
			$E("freeMsg2").style.display = "block";

			clearInterval(OutRoomTim);
			InRoomTim = setInterval(this.scanRoomDatas.bind(this), 500);
		};
		this.scanRoomDatas();
	};
	this.B_Player = function(PP) {
		if (!oSelf.on || (oSelf.Role.A && PP && oSelf.Role.A != PP)) {
			return false;
		} else {
			oRoom = this.openRoom(oSelf.on);
		};

		if (oSelf.Role.A) {
			if (!onClosing && this.onGame) {
				alert("B_Player:is gaming!");
				return false;
			};
			if (oRoom.ready[oSelf.Role.A]) {
				this.C_Ready();
			};
			oRoom[oSelf.Role.A] = false;
			oRoom.inTime[oSelf.Role.A] = new Date().getTime() + 1800000;
			this.saveRoom();
			this.scanRoomDatas();
			oSelf.Role = {};
			if (onClosing) alert("B-player:close");
		} else if (!oRoom[PP]) {
			oSelf.Role.A = PP;
			oSelf.Role.B = PAB[PP];
			oRoom[PP] = this.name;
			oRoom.inTime[PP] = new Date().getTime();
			this.saveRoom();
			this.scanRoomDatas();
		};
	};
	this.C_Ready = function() {
		if (!oSelf.Role.A) {
			return false;
		} else {
			oRoom = this.openRoom(oSelf.on);
		};
		if (oRoom.ready[oSelf.Role.A]) {
			if (!onClosing && oRoom.onGame) {
				alert("C_Ready:is gaming!");
				return false;
			};
			this.StopGame();
			if (onClosing) alert("C-ready:close");
		} else if (oRoom[oSelf.Role.B]) {
			useRand = -1;
			this.onGame = false;
			oRoom.onGame = false;

			if (oRoom.inTime[oSelf.Role.A] < oRoom.inTime[oSelf.Role.B]) {
				useRand = parseInt(Math.random() * 10) % 2;
				oRoom.rand = useRand;
				oRoom.frees = [];
				oRoom.gameTime.player1 = oRoom.setTimes;
				oRoom.gameTime.player2 = oRoom.setTimes;
			}; //↑ God Area ******---------- ↑ ------****************

			oRoom.ready[oSelf.Role.A] = true;
			this.saveRoom();
		};
		this.scanRoomDatas();
	};

	//    reading Free Area **********************************************
	this.clearUI = function() {
		for (var i = 0; i < childrens.length; i++) {
			childrens[i].style[bc] = "transparent";
		};
		this.oldFreeLen = 0;
		box = [];
	};
	this.downFreeSacn = function() {
		if (box[this.onPiece] != undefined) {
			return false;
		};
		this.oldFreeLen = oRoom.frees.length;
		box[this.onPiece] = (oRoom.frees.length % 2);
		childrens[this.onPiece].style[bc] = (oRoom.frees.length % 2 == 0 ? white : black);
		oSelf.goTiming = PAB[oSelf.goTiming];
		this.testWiner(this.onPiece);

		if (oRoom.onGame) {
			$E(oSelf.goTiming + "tim").style.color = "#D55";
			$E(PAB[oSelf.goTiming] + "tim").style.color = "#ACC";
		};
	};
	this.readFrees = function() {
		if (oRoom.frees.length > this.oldFreeLen && oRoom.frees.length % 2 == useRand) {
			this.onPiece = oRoom.frees[oRoom.frees.length - 1];
			this.downFreeSacn();
		};
	};
	this.downFree = function(inx) {
		this.onPiece = eval(inx);
		if (oRoom.frees.length % 2 == useRand) {
			oRoom.frees.push(this.onPiece);
			this.saveRoom();
			this.downFreeSacn();
		};
	};

	//    game exists  area  *******************************
	this.testWiner = function(inx) {
		var t = [],
			n = 0,
			myX = parseInt(inx / xyLen),
			itX, winReg = (oRoom.frees.length % 2),
			fullFree;
		this.winer = (winReg == 1 ? "黑子" : "白子");

		function getFullOrOver(drc, rev) {
			var ft = [];
			for (var i = 1; i < xyLen; i++) {
				n = inx + drc * i * rev;
				itX = parseInt(n / xyLen);
				if (Math.abs(itX - myX) > i || itX < 0 || itX >= xyLen || box[n] != winReg) {
					return ft;
				} else {
					ft.push(n);
				};
			};
		};

		function showFullOrOver(arr, sty) {
			if (sty == "#0F0" || (!fullFree && sty == "#0CC")) childrens[arr[0]].style.background = sty;
			for (var i = 1; i < arr.length; i++) {
				childrens[arr[i]].style.background = sty;
			};
		};
		for (var i = 0; i < 4; i++) {
			t[i] = [inx];
			t[i] = t[i].concat(getFullOrOver(arrDirc[i], 1));
			t[i] = t[i].concat(getFullOrOver(arrDirc[i], -1));
			if (t[i].length > 5) {
				/* 超出 */ } else if (t[i].length == 5) {
				this.StopGame(); /* 连满 */
				alert(this.winer + "获胜！");
				onGame = false;
			};
		};
		$E("showKey2").value = t.join("\n\n");
	};
	this.StopGame = function(type) {
		useRand = -1;
		this.onGame = false;
		$E("freeBnr2").innerHTML = "准备";
		$E("freeBnr2").style[bc] = "#777";
		if (oSelf.goTiming) {
			$E(oSelf.goTiming + "tim").style.color = "#ACC";
		};
		oRoom.ready = {};
		oRoom.onGame = false;
		this.saveRoom();
	};


	//    scan area *******************************************
	this.nolive = function(R, type) {
		if (type) {
			var t = this.openRoom(R);
			if (!t.time || t.time < (Ftime - 180000)) {
				deleteFolder(BigSrc + R);
			};
		};
	};
	this.startGame = function() {
		if (oRoom.inTime[oSelf.Role.A] > oRoom.inTime[oSelf.Role.B]) {
			useRand = contraryNUM[oRoom.rand];
		};
		oRoom.onGame = true;
		this.saveRoom();
		this.onGame = true;
		this.clearUI();
		oSelf.goTiming = (useRand == 0 ? oSelf.Role.A : oSelf.Role.B);
		$E(oSelf.goTiming + "tim").style.color = "#D55";
	};
	this.searchRooms = function() {
		var str = "",
			strRooms =
			"<div style=\u0022width:100px;height:20px;font-size:16px;background:#777;margin:1px 0px;cursor:pointer;color:white;text-align:center;\u0022 onclick=\u0022$E('freeLists3').innerHTML=this.innerHTML;\u0022>INNEROF</div>";
		var f = fso.getFolder(BigSrc);
		var d = new Enumerator(f.SubFolders);
		for (; !d.atEnd(); d.moveNext()) {
			this.nolive(d.item().name, true);
			str += strRooms.replace("INNEROF", d.item().name);
		};

		Ftime = new Date().getTime();
		$E("freeLists2").innerHTML = str;
		this.saveSelf();
	};
	this.scanRoomDatas = function() {
		oRoom = this.openRoom(oSelf.on);
		if (!oRoom.onGame) {
			this.showPlayers();
		};
		this.onGameExists();
		if (oRoom.onGame) {
			this.goTimer();
		};

		Ftime = new Date().getTime();
		this.saveSelf();
	};
	this.onGameExists = function() {
		if (!oSelf.Role.A) {
			$E("freeBnr2").innerHTML = "未入座";
			$E("freeBnr2").style[bc] = "#777";
		} else if (!oRoom[oSelf.Role.B]) {
			$E("freeBnr2").innerHTML = "等待入座";
			$E("freeBnr2").style[bc] = "#777";
			if (this.onGame) {
				this.StopGame();
			};
		} else if (!this.onGame) {
			if (oRoom.ready[oSelf.Role.A] && oRoom.ready[oSelf.Role.B]) {
				this.startGame();
			};
			this.showReadys();
		} else {
			this.readFrees();
		};
	};
	this.showPlayers = function() {
		if (oRoom.player1) {
			$E("player1").innerHTML = oRoom.player1;
			$E("player1").style[bc] = "#CCC";
		} else {
			$E("player1").innerHTML = "加入";
			$E("player1").style[bc] = "#777";
			$E(PA + "tim").innerHTML = "---";
		};
		if (oRoom.player2) {
			$E("player2").innerHTML = oRoom.player2;
			$E("player2").style[bc] = "#CCC";
		} else {
			$E("player2").innerHTML = "加入";
			$E("player2").style[bc] = "#777";
			$E(PB + "tim").innerHTML = "---";
		};
		if (oRoom.onGame) {
			return false;
		};

		if (oRoom.player1 && oRoom.player2) {
			if (oRoom.inTime.player1 < oRoom.inTime.player2) {
				$E(PA + "tim").innerHTML = "房主";
			} else {
				$E(PB + "tim").innerHTML = "房主";
			};
		} else if (oRoom.player1 || oRoom.player2) {
			if (oRoom.player1) {
				$E(PA + "tim").innerHTML = "房主";
			} else {
				$E(PB + "tim").innerHTML = "房主";
			};
		};
	};
	this.showReadys = function() {
		if (!oRoom.ready[oSelf.Role.A]) {
			$E("freeBnr2").innerHTML = "新局发起";
			$E("freeBnr2").style[bc] = "#CCC";
		} else {
			$E("freeBnr2").innerHTML = oRoom.ready[oSelf.Role.B] ? (useRand == 1 ? "对方先" : "我方先") : "我方就绪";
			$E("freeBnr2").style[bc] = "#7F7";
		};
		$E(oSelf.Role.A).style[bc] = "#CC7";
	};
	this.goTimer = function() {
		if (!this.onGame) {
			if (oSelf.saveTimes != oRoom.setTimes) {
				oRoom.setTimes = (oRoom.setTimes < 180 ? 180 : oRoom.setTimes > 1800 ? 1800 : oRoom.setTimes);
				oSelf.saveTimes = oRoom.setTimes;
				this.saveRoom();
			};
			if (oRoom.ready.player1) {
				$E("player1tim").innerHTML = parseTime(oRoom.setTimes);
			};
			if (oRoom.ready.player2) {
				$E("player2tim").innerHTML = parseTime(oRoom.setTimes);
			};
		} else {
			if (oSelf.Role.A == oSelf.goTiming) {
				oRoom.gameTime[oSelf.goTiming] -= 0.5;
				this.saveRoom();
			};
			$E(oSelf.goTiming + "tim").innerHTML = parseTime(oRoom.gameTime[oSelf.goTiming]);
		};
	};


	//    init area *******************************************
	var strBackgroundUI =
		"<span style=\u0022width:(1)px;height:(1)px;float:left;border:1px solid black;border-radius:1px;\u0022></span>";
	var strFreesUI =
		"<span style=\u0022width:(1)px;height:(1)px;float:left;border:none;border-radius:50%;\u0022 value=\u0022valueInr\u0022 onclick=\u0022FREE.downFree(this.getAttribute('value'));\u0022></span>";
	this.initBoxUI = function(siz, len) {
		$E("freeBox1").innerHTML = "";
		$E("freeBox2").innerHTML = "";
		var str1 = "",
			str2 = "";
		xyLen = len;
		arrDirc = [1, len + 1, len - 1, len]
		var strA = strBackgroundUI.replace(/\(1\)/g, siz - 2);
		var strB = strFreesUI.replace(/\(1\)/g, siz);
		for (var i = 0; i < len * len; i++) {
			if (i <= (len * (len - 2))) {
				str1 += strA;
			};
			str2 += strB.replace("valueInr", i);
		};
		Stys.set("freeBox1", "border:1px solid black;width:AOFpx;height:AOFpx;top:BOFpx;left:BOFpx;".replace(/AOF/g,
			siz * (len - 1)).replace(/BOF/g, parseInt($E("freeBox2").parentNode.offsetHeight / 2 - (siz * (
			len - 1) / 2))));
		Stys.set("freeBox2", "width:AOFpx;height:AOFpx;top:BOFpx;left:BOFpx;".replace(/AOF/g, siz * len).replace(
			/BOF/g, parseInt($E("freeBox2").parentNode.offsetHeight / 2 - (siz * len / 2))));
		$E("freeBox1").innerHTML = str1;
		$E("freeBox2").innerHTML = str2;
		childrens = $E("freeBox2").children;
	};

	//  close Area *********************** *************** *************** ******************
	this.close = function(typ) {
		var t = 0;
		try {
			onClosing = true;
			t = 1;
			if (oSelf.on) this.A_Guest(oSelf.on);
			t = 2;
			deleteFile(BigSrc + this.name + Txt);
		} catch (e) {
			alert("Errors", JSON.stringify(returnError(e)));
		};
	};

	//    init this area  ************
	if (this.name) {
		OutRoomTim = setInterval(this.searchRooms.bind(this), 1000);
	} else {
		$E("freeMsg1").innerHTML = "游客 无名";
	};
};